//
//  CZStatusCell.swift
//  sinaWeiboswift
//
//  Created by 段文杰 on 16/4/14.
//  Copyright © 2016年 段文杰. All rights reserved.
//

import UIKit
import SnapKit
class CZStatusCell: UITableViewCell {

    
    var status : CZStatusModel? {
        didSet{//将VC的status值再一次传递给oringnaView
            oringinaView.status = status
        
            //先将之前的bottomView的约束卸载掉
            bottomViewTopCons?.uninstall()
            
            if let retweetStatus = status?.retweeted_status {
            
            //有值
                retweetedPictureView.hidden = false
                retweetedPictureView.retweetedStatus = retweetStatus
                
                botomView.snp_makeConstraints(closure: { (make) in
                    bottomViewTopCons = make.top.equalTo(retweetedPictureView.snp_bottom).constraint
                })
                
            }else {
            
            //没有值
                retweetedPictureView.hidden = true
                botomView.snp_makeConstraints(closure: { (make) in
                    bottomViewTopCons = make.top.equalTo(oringinaView.snp_bottom).constraint
                })
            
            }
            
        }
    
    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: .Default, reuseIdentifier: "cell")
        
        self.backgroundColor = UIColor.init(red: ( CGFloat(arc4random_uniform(256)) / 255.0), green: ( CGFloat(arc4random_uniform(256)) / 255.0), blue: ( CGFloat(arc4random_uniform(256)) / 255.0), alpha: 1.0)
//        self.backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1)

        setUI()
    }
    
    private func setUI(){
    
        //添加子控件
    
       self.contentView.addSubview(oringinaView)
    
        self.contentView.addSubview(retweetedPictureView)
        
        self.contentView.addSubview(botomView)
        
        
        
        //设置约束
        
        
       
        oringinaView.snp_makeConstraints { (make) in
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(contentView).offset(10)
            // 高度参照子控件的高度
//        make.height.equalTo(Int(arc4random_uniform(200)) + 50)
        }

        retweetedPictureView.snp_makeConstraints { (make) in
            make.top.equalTo(oringinaView.snp_bottom)
            make.leading.trailing.equalTo(contentView)
            
        }
        
        
        //设置botoViewframe值
        botomView.snp_makeConstraints { (make) in
            make.leading.trailing.equalTo(contentView)
            make.height.equalTo(40)
            
         bottomViewTopCons = make.top.equalTo(retweetedPictureView.snp_bottom).constraint
                   }
    
        contentView.snp_makeConstraints { (make) in
            make.leading.trailing.top.equalTo(self)
           
//            make.height.equalTo(oringinaView).offset(10)
            make.bottom.equalTo(botomView)
        }
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    

    
    // MARK: - ******************* #懒加载# ********************
  
    private  var bottomViewTopCons: Constraint?
    private lazy var retweetedPictureView: CZStatusretweetedView = CZStatusretweetedView()

    private lazy var oringinaView :CZStatusoriginnaView = CZStatusoriginnaView()
    private lazy var botomView : CZStatusBottomView = CZStatusBottomView()
    
}



